package com.yuneec.fly.views;


import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import com.yuneec.fly.R;

public class ZoomOperationView extends LinearLayout {
    private static final String TAG = "ZoomOperationView";
    public static final int ZOOM_OPERATION_STOP = 0;
    public static final int ZOOM_OPERATION_TELE = 1;  //42
    public static final int ZOOM_OPERATION_WIDE = 2;  //14

    //	private Button mBtnWide;
//	private Button mBtnTele;
//	private TextView mTxtZoomMax;
//	private TextView mTxtZoomMin;
    private TextView mValueTxt;

    private String valueMax;
    private String valueMin;
    private String value;

    ViewWrapper viewWrapper;
    ObjectAnimator translationOut;
    ObjectAnimator translationIn;
    private boolean zoomOut;

    public ZoomOperationView(Context context, AttributeSet attrs,
                             int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    public ZoomOperationView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ZoomOperationView(Context context) {
        this(context, null);
    }

    private void init(Context context, AttributeSet attrs,
                      int defStyleAttr) {
        TypedArray typeArray = context.obtainStyledAttributes(attrs, R.styleable.ZoomOperationView);
        valueMax = typeArray.getString(R.styleable.ZoomOperationView_textMax);
        valueMin = typeArray.getString(R.styleable.ZoomOperationView_textMin);
        value = typeArray.getString(R.styleable.ZoomOperationView_text);

        LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        li.inflate(R.layout.combination_button_layout, this, true);
        zoomOut = false;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mValueTxt = (TextView) findViewById(R.id.zoom_txt_current);
        if (value != null) {
            mValueTxt.setText(value);
        }

        zoomClick();
        ((SeekBar) findViewById(R.id.sb_zoom_focus)).setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(final SeekBar seekBar) {
                // TODO Auto-generated method stub
                if (seekBar.getProgress() != 50) {
                    int[] count = {seekBar.getProgress(), 50};
                    ObjectAnimator animator = ObjectAnimator.ofInt(new Object(), "", count);
                    animator.setDuration(300);
                    animator.setInterpolator(new AccelerateInterpolator(1));
                    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                        @Override
                        public void onAnimationUpdate(ValueAnimator animation) {
                            if (seekBar != null) {
                                seekBar.setProgress((Integer) animation.getAnimatedValue());
//	                                seekBar.setProgress(50);
                            }
                        }
                    });
                    animator.start();
                }
                if (mZoomOperatedListener != null) {
                    mZoomOperatedListener.onOperated(ZOOM_OPERATION_STOP);
                }

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                // TODO Auto-generated method stub
                int oper = -1;
                if (progress >= 0 && progress < 50) {
                    if (progressTemp > progress) {
                        oper = ZOOM_OPERATION_TELE;
                    }
                }
                if (progress > 50 && progress <= 100) {
                    if (progressTemp < progress) {
                        oper = ZOOM_OPERATION_WIDE;
                    }
                }
                progressTemp = progress;
                if (mZoomOperatedListener != null) {
                    if (oper != -1) {
                        mZoomOperatedListener.onOperated(oper);
                    }
                }
            }
        });
        findViewById(R.id.buttonLayout).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (zoomOut) {
                    zoomOut = false;
                    zoomIn();
                } else {
                    zoomOut = true;
                    zoomOut();
                }
            }
        });
    }

    int progressTemp = 50;

    private void zoomClick() {
        viewWrapper = new ViewWrapper(findViewById(R.id.seekBarLayout));
        PropertyValuesHolder setHeight1 = PropertyValuesHolder.ofInt("height", 0, 145);
        translationOut = ObjectAnimator.ofPropertyValuesHolder(viewWrapper, setHeight1).setDuration(300);
        translationOut.setDuration(1000);

        PropertyValuesHolder setHeight2 = PropertyValuesHolder.ofInt("height", 145, 0);
        translationIn = ObjectAnimator.ofPropertyValuesHolder(viewWrapper, setHeight2).setDuration(300);
        translationIn.setDuration(1000);
    }

    public void zoomOut() {
        translationOut.start();
    }

    public void zoomIn() {
        translationIn.start();
    }


    public void setCurrentValue(String value) {
        mValueTxt.setText(value);
    }

    public void setMaxValue(String value) {
        mValueTxt.setText(value);
    }

    public void setMinValue(String value) {
        mValueTxt.setText(value);
    }

    public interface onZoomOperatedListener {
        public void onOperated(int operate);
    }

    private onZoomOperatedListener mZoomOperatedListener;

    public void setOnZoomChangedListener(onZoomOperatedListener zoomOperatedListener) {
        mZoomOperatedListener = zoomOperatedListener;
    }
}
